package com.wn.budget.controller;

import ch.qos.logback.core.util.DatePatternToRegexUtil;
import com.wn.budget.code.DateUtil;
import com.wn.budget.code.context.ContextManager;
import com.wn.budget.model.Admin;
import com.wn.budget.model.Project;
import com.wn.budget.repository.ProjectRepository;
import com.wn.budget.service.ProjectService;
import com.wn.budget.service.TemplateService;
import com.wn.budget.vo.ProjectData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;
import java.util.UUID;

/**
 * 工程
 */
@RestController
@RequestMapping(value = "project")
public class ProjectController {

    @Autowired
    private ProjectRepository projectRepository;

    @Autowired
    private TemplateService templateService;

    @Autowired
    private ProjectService projectService;

    @PostMapping("insert")
    public boolean insert(HttpServletRequest request, Project project) {
        project.setId(UUID.randomUUID().toString());
        project.setCreateDate(new Date());

        Admin admin = ContextManager.getLoginUser(request);
        project.setAdminId(admin.id);
        projectRepository.saveAndFlush(project);

        templateService.initTemplate(project.getId(), project.getTemplateId());
        return true;
    }

    @PostMapping("update")
    public boolean update(Project project) {
        projectRepository.saveAndFlush(project);
        return true;
    }

    @GetMapping("findById")
    public Project findById(@RequestParam("id") String id) {
        return projectRepository.findById(id).get();
    }

    @GetMapping("deleteById")
    public void deleteById(@RequestParam("id") String id) {
        projectRepository.deleteById(id);
    }

    @GetMapping("findAll")
    public Page<Project> findAll(HttpServletRequest request, @RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize) {
        Admin admin = ContextManager.getLoginUser(request);
        Page<Project> page = projectRepository.findProjectByAdminId(admin.getId(), PageRequest.of(pageIndex, pageSize));
        return page;
    }

    @GetMapping("findProjects")
    public Page<Project> findProjects(HttpServletRequest request,
                                      @RequestParam("pageIndex") int pageIndex,
                                      @RequestParam("pageSize") int pageSize,
                                      @RequestParam("customerName") String customerName,
                                      @RequestParam("beginDate") String beginDate,
                                      @RequestParam("endDate") String endDate) throws ParseException {
        Admin admin = ContextManager.getLoginUser(request);
        Date beginDate1 = DateUtil.toDate(beginDate);
        Date endDate2 = DateUtil.toDate(endDate);
        Page<Project> page = projectService.findProjects(admin.getId(),
                pageIndex, pageSize, beginDate1, endDate2, customerName);
        return page;
    }

    @ApiOperation(value = "打印工程材料")
    @GetMapping("printProject")
    public ProjectData printProject(HttpServletRequest request,
                                    @RequestParam("projectId") String projectId) {
        return projectService.findProjectData(projectId);
    }
}
